
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  sms;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Timer;

import logger.PoolLogger;

import dbmanager.DBManager;
import file.FileWriter;
import utility.Input;
import utility.MyDate;

public class SmsBuffer extends Thread
{	
	String path;
	int delay = 1000;   // delay for 5 sec.
    Timer timer = new Timer();
    
	FileWriter raf =null;
	String filePath;
	PoolLogger pl;
	
	public SmsBuffer() 
	{
		path = Input.SMSBUFFER;
		createLog();
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	void createLog()
	{
		ResultSet rs = null;
		try 
		{
			MyDate date = new MyDate();
			String today_date = date.getDate();
			
			rs = DBManager.getSelect("SELECT * FROM sms where status =1 and datetime <"+today_date);
			rs.next();
			String check = rs.getString("id");
			rs.previous();
			try 
			{
				filePath = path+today_date+Input.FILRFORMAT;
				raf = new FileWriter(filePath,"rws");
				raf.fileOpen();
			} catch (Exception e) {
			}
			if(raf.file.length()==0)
			{
				raf.file.writeBytes("Index\tMobile no\tDate time\tMessage\tStatus\n");
			}
			else
			{
				raf.file.seek(raf.file.length());
				String IdString = "";
				try
				{
					while(rs.next())
					{
						String index = rs.getString("id").toString();
						IdString = index + ",";
						String Mobileno = rs.getString("Mobile");
						String msg = rs.getString("message");
						today_date = rs.getString("datetime");
						raf.file.writeBytes(index+"\t"+Mobileno+"\t"+today_date+"\t"+msg+"\t"+"N"+"\n");
   					}
				} 
				catch (SQLException e) {
					e.printStackTrace();
				}
				IdString = IdString.substring(0, IdString.length()-1);
				DBManager.getDelete("Delete from sms where id in ("+IdString+")");
			    rs.close();
			    raf.fileClose();
			}
			
		}
		catch (Exception e) {
		}
	}
	
	public synchronized  boolean addBufferLog(String Mobileno,String msg)   
	{
		if(msg.contains("$noresponse$"))
			return true;
		Hashtable<Object, Object> table = new Hashtable<Object, Object>();
		table.put("mobile", Mobileno);
		table.put("message", msg);
		table.put("send", "1");		
		DBManager.getInsert("sms", table);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public synchronized void SendSMSFromBuffer() 
	{	
		SendSMS sendMessageToMobile = new SendSMS();
		while(true)
		{
			try 
			{
				sendMessageToMobile.sendSMS();
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			try 
			{
				Thread.sleep(delay);
			}
			catch (Exception e) {
			}
		}
	}
	
	public static void main(String[] args) 
	{
		SmsBuffer sb = new SmsBuffer();
		sb.addBufferLog("+919922918602", "hi marutee, testing 'smsBuffer");
		//sb.SendSMSFromBuffer();
	}	
	
	public void run() 
	{
			try
			{
				SendSMSFromBuffer();

			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
	}
	
}
